feat(metrics): Add implementation for metrics envelope item#6960
feat(metrics): Add implementation for metrics envelope item#6960
Conversation
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #6960 +/- ##
=============================================
+ Coverage 84.868% 85.472% +0.603%
=============================================
Files 462 465 +3
Lines 27936 28036 +100
Branches 12351 12392 +41
=============================================
+ Hits 23709 23963 +254
+ Misses 4186 4031 -155
- Partials 41 42 +1
... and 13 files with indirect coverage changes Continue to review full report in Codecov by Sentry.
|
a5056fb to
1210307
Compare
02c3573 to
303d533
Compare
- Introduced new test file `SentryItemBatcherTests.swift` to validate the behavior of the `SentryItemBatcher`. - Added tests for various scenarios including item addition, buffer size limits, timeout handling, and attribute enrichment. - Updated project configuration to include the new test file in the build settings.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
- Updated `TestCurrentDateProvider` to include `_absoluteTime` tracking during date advancements. - Modified `Batcher` to clarify synchronous behavior in documentation and ensure correct item count is passed to the callback. - Added tests to verify scope attribute application and item count during capture in `BatcherScopeTests` and `BatcherTests`.
Tests/SentryTests/Integrations/Metrics/SentryMetricsBatcherTests.swift
Outdated
Show resolved
Hide resolved
Tests/SentryTests/Integrations/Metrics/SentryMetricsIntegrationTests.swift
Outdated
Show resolved
Hide resolved
|
Thanks @philipphofmann, I'll keep this PR open and ready-for-merge until #6957 is ready to be merged too (blocked by #7077) so I merge all three PRs directly after another. |
…teContent directly - Changed the type alias for attributes in SentryMetric from SentryAttribute to SentryAttributeContent. - Simplified the attributes dictionary handling in SentryMetric by removing unnecessary mapping. - Updated tests to reflect the new attribute handling, ensuring consistency across metric creation and attribute assignment.
Samples/SentrySampleShared/SentrySampleShared/SentrySDKWrapper.swift
Outdated
Show resolved
Hide resolved
When a metric is emitted while a transaction or span is active, the metric's traceId was incorrectly sourced from the propagationContext, while span_id was correctly sourced from the active span. This caused a mismatch when the propagationContext had a different traceId than the active span, breaking correlation between metrics and their originating transaction in distributed tracing scenarios. The fix ensures that when a span is active, traceId comes from the span (consistent with span_id), otherwise falls back to propagationContext traceId. Added test cases to verify this behavior.
…alizer Updated the assignment of the custom attribute in SentryMetric to utilize the .string initializer instead of the .init method, ensuring consistency with the new attribute handling approach.
The test was failing because it created a span without setting its traceId. Since the fix now uses span.traceId when a span is active, the test needs to set the span's traceId to match the propagationContext traceId for consistency.
Semver Impact of This PR🟡 Minor (new features) 📋 Changelog PreviewThis is how your changes will appear in the changelog. New Features ✨Metrics
Other
Bug Fixes 🐛
Build / dependencies / internal 🔧Deps
Other
Other
🤖 This preview updates automatically when you update the PR. |
This PR is part of a merge-chain and should be merged one-by-one into
mainas soon as all of them are ready to be merged:📜 Description
This PR implements the metrics envelope item functionality for the Sentry Cocoa SDK. It adds the core infrastructure for capturing, batching, and sending metrics to Sentry.
Key Changes
SentryMetric Protocol: Added a new
SentryMetricclass that represents a metric entry with support for:SentryMetricBatcher: Implemented a batcher that:
beforeSendMetriccallback for filtering/modifying metricsMetricsIntegration Updates: Enhanced the integration to:
addMetric(_:scope:)API for adding metricsClient Support: Added
captureMetricsData(_:with:)method toSentryClientthat:trace_metricapplication/vnd.sentry.items.trace-metric+jsonData Category Support: Added
trace_metricdata category to:SentryDataCategoryenumSentryDataCategoryMapperfor mapping envelope item types to categoriesSentryEnvelopeItemTypefor the envelope item type constantOptions: Added
beforeSendMetriccallback option for filtering/modifying metrics before sendingTesting
Added comprehensive test coverage:
captureMetricsDataenvelope creation💡 Motivation and Context
Closes #6948
Closes #6951
Closes #6952
💚 How did you test it?
SentryMetriccovering encoding, attribute management, and metric typesSentryMetricBatchercovering batching, flushing, timeout behavior, attribute enrichment, and edge casesMetricsIntegrationandSentryClientmetrics capture📝 Checklist
You have to check all boxes before merging:
sendDefaultPIIis enabled.